<template>
  <el-dialog title="任务改派" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="changeForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="请选择改派人" label-width="100px">
        <el-select v-model="form.assignee" @change="select_status"  style="width:100%">
          <el-option
            v-for="item in data"
            :key="item.objectId"
            :label="item.usrName"
            :value="item.objectId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('changeForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import TreeSelect from "@/components/basic-treeselect/treeselect";
import { activeUrl,baseUrl } from "@/config/env";
const urls = {
  change: activeUrl + "/changeAssignee",
  list : baseUrl + "/cp/api/list"
};
export default {
  data() {
    return {
      dialogVisible: false,
      data: [],
      defaultCheckedKeys: [],
      form: {
        taskId: null,
        assignee: null,
      },
      rules: {
        
      },
      formInline: {
        name: null,
        current: 1, // 当前页
        size: 10, // 每页条数
        total: 0, // 总条数
        orders: []   
      },
    };
  },
  components: {
    TreeSelect
  },
  methods: {
    popoverHide(checkedIds, checkedData) {
      this.form.menus = checkedIds.toString();
    },
    show(id) {
      this.dialogVisible = true;
      if (this.$refs["changeForm"]) {
        this.$refs["changeForm"].resetFields();
      }
      this.fetch(id);
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if(this.form.assignee) {
            axios.get(urls.change, {params : this.form}).then(res => {
              this.$message(res.data.result);
              this.dialogVisible = false;
              this.$emit("refresh");
            });
          }else {
            this.$message({
              message : "请先选择要改派的人",
              type : "warning"
            })
          }
        }
        return false;
      });
    },
    fetch(id) {
      this.form.taskId = id;
       axios.post(urls.list,this.formInline).then(res => {
        this.data = res.data.result;
      })
    },
    select_status(e) {
      this.form.assignee = e;
    }
  }
};
</script>